package com.gnerv.pabu.test.user;

import com.gnerv.pabu.test.user.service.OrgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableDiscoveryClient
@CrossOrigin
@RestController
@EnableFeignClients
public class UserApplication {

    @LoadBalanced
    @Bean
    public RestTemplate loadbalancedRestTemplate(){
        return new RestTemplate();
    }

    @Value("${server.port}")
    private String port;


    @Value("${spring.cloud.consul.host}")
    private String consulHost;

    @Autowired
    RestTemplate restTemplate;

    @Autowired
    OrgService orgService;

    public static void main(String[] args) {
        SpringApplication.run(UserApplication.class, args);
    }

    @RequestMapping(value = "get/{name}", method = RequestMethod.GET)
    public String get(@PathVariable(value = "name") String name) {
        String s1 = orgService.get(name);
        System.out.println(s1);
        String s = this.restTemplate.getForObject("http://pabu-org/get/" + name, String.class);
        System.out.println(s);
        return name + "---" + port + "---" + consulHost + "+++" + s;
    }

}
